﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DBHelper;
using System.Data.SqlClient;
using System.Data;
using BusinessLayer.FindArtInfo.Entities;
using BusinessLayer.FindArtInfo.Models;

namespace BusinessLayer.FindArtInfo
{
    public class ArtWorkBL
    {
        public List<ArtWork> GetByPriceId(int priceId)
        {
            List<ArtWork> result = new List<ArtWork>();
            var adapter = new BaseDAO(DbConfig.DbFindArtInfoConnectionString);                
            adapter.AddParameter("@PriceId", priceId);
            try
            {
                result = adapter.GetList<ArtWork>("sp_ArtWork_Select_By_PriceId", CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);                
            }
            catch (Exception)
            {
            }
            finally { adapter.Dispose(); }
            return result;
        }
        
        public ArtWork GetById(int id)
        {
            ArtWork result = new ArtWork();
            var adapter = new BaseDAO(DbConfig.DbFindArtInfoConnectionString);
            adapter.AddParameter("@Id", id);
            try
            {
                result = adapter.GetList<ArtWork>("sp_ArtWork_Select_By_Id", CommandType.StoredProcedure, CustomConnectionState.CloseOnExit).FirstOrDefault();
            }
            catch (Exception)
            {
            }
            finally { adapter.Dispose(); }
            return result;
        }

        public List<ArtWorkDetailExtend> GetByItemType(int? itemType, int pageIndex, int pageSize, int? sort)
        {
            List<ArtWorkDetailExtend> result = new List<ArtWorkDetailExtend>();
            var adapter = new BaseDAO(DbConfig.DbFindArtInfoConnectionString);
            adapter.AddParameter("@ItemType", itemType);
            adapter.AddParameter("@PageIndex", pageIndex);
            adapter.AddParameter("@PageSize", pageSize);
            adapter.AddParameter("@Sort", sort);
            try
            {
                result = adapter.GetList<ArtWorkDetailExtend>("sp_ArtWork_Select_By_ItemType", CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
            }
            catch (Exception)
            {
            }
            finally { adapter.Dispose(); }
            return result;
        }
        
        public void GetCountByItemType(int? itemType, out int totalRecord)
        {
            totalRecord = 0;
            var adapter = new BaseDAO(DbConfig.DbFindArtInfoConnectionString);
            adapter.AddParameter("@ItemType", itemType);
            var output= adapter.AddParameter("@TotalRecord", SqlDbType.Int,ParameterDirection.Output);
            try
            {
                adapter.ExecuteNonQuery("sp_ArtWork_Select_Count_By_ItemType", CommandType.StoredProcedure, CustomConnectionState.CloseOnExit);
                totalRecord = (int)output.Value;
            }
            catch (Exception)
            {
            }
            finally { adapter.Dispose(); }
        }        
    }
}
